package controller;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import model.Model;
import model.entity.BasicEntity;
import model.item.EquipmentSlot;
import model.item.EquippedInventory;
import model.item.TakeableItem;
import view.EquipmentOverlay;
import view.View;

public class EquipmentController extends Controller {

	private BasicEntity avatar;
	private EquipmentOverlay eo;
	
	public EquipmentController( BasicEntity avatar, EquipmentOverlay eo ) {
		this.avatar = avatar;
		this.eo = eo;
		this.eo.register( this );
		this.update( eo );
	}
	
	@Override
	public void update(View v) {
		if( eo != v ) { return ; }
		EquippedInventory ei = avatar.getEquipment();
		if( ei == null ) { return ; }
		Map<Model, MouseListener> m = new HashMap<Model, MouseListener>();
		Set<EquipmentSlot> keys = ei.getEquipment().keySet();
		for( EquipmentSlot es : keys ) {
			final TakeableItem item = ei.getEquipment( es );
			m.put( item , new MouseListener() {
				@Override
				public void mouseClicked(MouseEvent e) {
					item.use( avatar );
				}
				@Override
				public void mouseEntered(MouseEvent e) {}
				@Override
				public void mouseExited(MouseEvent e) {}
				@Override
				public void mousePressed(MouseEvent e) {}
				@Override
				public void mouseReleased(MouseEvent e) {}
			} );
		}
		eo.setListeners( m );
	}

	
}
